package hk.edu.vtc.sea.jsonClient;

import java.util.ArrayList;
import java.util.HashMap;

import org.springframework.web.client.RestTemplate;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class JsonClientActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
    	//Remove the below 2 line of code, if you are not using VTC network!
    	System.setProperty("http.proxyHost", "10.0.2.2");
    	System.setProperty("http.proxyPort", "8888");

        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.main);
        getUpdates.start();
    }

    private Thread getUpdates = new Thread() {
        public void run() {
            String display = "";
            try {
                RestTemplate restTemplate = new RestTemplate();
                String query = "nomnomnom";
                int count = 3;
                String url = "http://localhost.:2323/data/one/1";
                Person response = restTemplate.getForObject(url,
                		Person.class);                
                display += "@" + response.getId() + ": " + response.getName() + "\n\n";
                
                url = "http://localhost.:2323/data/All/";
                Person[] allPeople = restTemplate.getForObject(url,
                		Person[].class);  
                
                for (Person person : allPeople) {
                	display += "@" + person.getId() + ": " + person.getName() + "\n";
				}
               
//                String url = "http://search.twitter.com/search.json" +
//                        "?q=#{query}&p=1&rpp={count}" +
//                        "&result_type=recent&lang=en";
//                HashMap response = restTemplate.getForObject(url,
//                        HashMap.class, query, count);
//                ArrayList<HashMap> results = (ArrayList<HashMap>) response
//                        .get("results");
//                for (HashMap tweets : results) {
//                    String tweet = (String) tweets.get("text");
//                    String author = (String) tweets.get("from_user");
//                    display += "@" + author + ": " + tweet + "\n";
//                }
            } catch (Exception e) {
                e.printStackTrace();
                display = e.getMessage();
            }
            final String toShow = display;

            runOnUiThread(new Runnable() {
                public void run() {
                    ((TextView) findViewById(R.id.text)).setText(toShow);
                }
            });
        }
    };
}